package com.pxmeta.app.aspect;

import cn.dev33.satoken.stp.StpUtil;
import com.google.gson.Gson;
import com.pxmeta.common.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Aspect
@Component
@Slf4j
public class UserLogAspect {

    @Pointcut("@annotation(cn.dev33.satoken.annotation.SaCheckLogin)")
    public void userLog() {}


    @Before("userLog()")
    public void doBefore(JoinPoint joinPoint){
        try{
            Long userId = SecurityUtils.getUserId();
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
            log.info("PengXin用户行为记录 => 当前登录用户id: {}, 正在访问: {}",userId,request.getRequestURL().toString());
        }catch (Exception e){
            log.error("PengXin用户行为记录 => 日志记录失败,错误信息:{}",e.getMessage());
        }

    }

//    @Around("execution(* com.pxmeta.app.controller..*.*(..))")
//    public Object logExecutionTime(ProceedingJoinPoint joinPoint) {
//
//        try {
//            long startTime = System.currentTimeMillis();
//            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//            HttpServletRequest request = attributes.getRequest();
//
//            String url = request.getRequestURL().toString();
//            if (joinPoint == null){
//                return null;
//            }
//            Object result = joinPoint.proceed();
//
//
//            long endTime = System.currentTimeMillis();
//            long executionTime = endTime - startTime;
//
//            log.info("url: {} ,接口调用时间: " + executionTime + "ms",url);
//            return result;
//        } catch (Throwable ignored){
//
//        }
//
//        return null;
//
//    }

}
